#include "gensud.hpp"

using namespace std;

/* --------------------------------------------------------------- */
Sudoku * GenSud::getSudoku() {
	cout << "GenSud::getSudoku() ENTER" << endl;
	//TODO: zoptymalizowac uzywanie wskaznikow, dodac klase przechowujaca wszystkie obiekty etapu tworzenia sudoku (zrodlo, maska, maskarada)
	int i;
	Sudoku* sol = NULL;
	Sudoku * ret = new Sudoku();
	std::vector<Sudoku *> * list;
	int attempts = 0, noSol = 0;
	Board masq;
	
	//while(noSol != 1) {
		
		//if(list)
			//list->clear();
		
		ret->reset();
		ret->fill();
		cout << "Filled sudoku: " << endl;
		ret->print();
	
		list = ret->findSolution(0);
		cout << "Solved sudoku: list.size()=" << list->size() << endl;
		ret = (*list)[0];
		cout << "ret=" << ret << endl;
		ret->print();		
		
		masq.reset();
		getMasq(&masq, 60);
		//cout << "Masq" << endl;
		masq.print();	
	
		makeMasq(ret, &masq);
	/*
		cout << "Masqarade sudoku: " << endl;
		ret->print();
				
		list->clear();
	
		list = ret->findSolution(10);
		noSol = list->size();
		cout << "Solved sudoku: list.size=" << list->size() << " attempts=" << ++attempts << endl;
		

	//}
		
	ret = new Sudoku((*list)[0]); //copy element to return
	ret->print();
	
	//empty of list
	while(!list->empty()) {
		//delete list->back();
		list->pop_back();
	}
		*/
	return ret;
}
/* --------------------------------------------------------------- */
void GenSud::makeMasq(Sudoku * sudoku, Board * masq) {
	cout << "dupa1" << endl;
	int i, j;
	for(i = 0; i < ROWS; i++) 
		for(j = 0; j < COLUMNS; j++)
			if(masq->tab[i][j] == 0) {
				//cout << "masq->tab["<<i<<"]["<<j<<"]="<<masq->tab[i][j]<<endl;
				//sudoku->getField(i, j)->setValue(0);
				sudoku->setField(0, i, j);
			}
	cout << "dupa2" << endl;
}
/* --------------------------------------------------------------- */
void GenSud::getMasq(Board * masq, int numberOfElements) {
	Random random;
	int rr, rc;
	cout << "getMasq: ENTER" << endl;
	for(int i = 0; i < numberOfElements; i++) {
		rr = random.getInt(0, ROWS);
		rc = random.getInt(0, COLUMNS);
		while(masq->tab[rr][rc] != 0) {
			rr = random.getInt(0, ROWS);
			rc = random.getInt(0, COLUMNS);
		}
		masq->tab[rr][rc] = 1;
	}
	cout << "getMasq: EXIT" << endl;
}
/* --------------------------------------------------------------- */
